<?php

namespace app\htsoft\controller;

use app\common\controller\Base;
use think\captcha\Captcha;
use think\Controller;
use think\Db;
use think\Request;

class Index extends Base
{
    public function index()
    {
        $id = session("admin_auth")["admin_id"];
        $user = model("AdminUsers")->where(["id" => $id])->find();
        $project_name = model("Options")->value("project_name");
        $where["user_id"] = $id;
        $rolename = DB::name("Role")->alias("r")->join(DB_PREFIX . "role_user o", "r.id = o.role_id")->where($where)->value('name');
//        dump(model("Menu")->menu_json());die;
        $submenus = model("Menu")->menu_json();
//        dump($this->getsubmenu($submenus));die;
        $this->assign("rolename", $rolename);
        $this->assign("SUBMENU_CONFIG", $this->getsubmenu($submenus));
        $this->assign("project_name", $project_name);
        $this->assign("user", $user);
        return $this->fetch();
    }

    /**
     * 一级菜单
     * @param $submenus
     * @return string
     */
    public function getsubmenu($submenus)
    {
        $str ="";
        foreach ($submenus as $menu) {
            $str .= "<li>";
            if(empty($menu["items"])){
                $str.= "<a href=\"$menu[url]\" class=\"J_menuItem\">
				<i class=\"$menu[icon] |default='fa fa-table'\"></i>
				<span class=\"menu-text\">$menu[name]</span></a>";
            }else{
                $str.= "<a href=\"#\" class=\"dropdown-toggle\" class=\"J_menuItem\">
				<i class=\"$menu[icon] |default='fa fa-table' normal\"></i>
				<span class=\"menu-text normal\">$menu[name]</span>
				<b class=\"arrow fa fa-angle-right normal\"></b></a>";
                $str.= "<ul  class=\"nav nav-second-level\">";
                $str.= $this->getsubmenu1($menu["items"]);
                $str.= "</ul>";
            }
            $str.= "<li>";
        }
        return $str;
    }

    /**
     * 二级菜单
     * @param $submenus
     * @return string
     */
    public function getsubmenu1($submenus)
    {
        $str = "";
        foreach ($submenus as $menu) {
            $str.= "<li>";
            if(empty($menu["items"])){
                $str.= "<a href=\"$menu[url]\" class=\"J_menuItem\">
				<i class=\"fa fa-caret-right\"></i>
				<span class=\"menu-text\">$menu[name]</span></a>";
            }else{
                $str.= "<a href=\"#\" class=\"dropdown-toggle\" class=\"J_menuItem\">
				<i class=\"fa fa-caret-right\"></i>
				<span class=\"menu-text\">$menu[name]</span>
				<b class=\"arrow fa fa-angle-right\"></b></a>";

                $str.= "<ul  class=\"nav nav-third-level\">";
                $str.= $this->getsubmenu2($menu["items"]);
                $str.="</ul>";
            }
            $str .= "<li>";
        }
        return $str;
    }

    /**
     * 三级菜单
     * @param $submenus
     * @return string
     */
    public function getsubmenu2($submenus)
    {
        $str = "";
        foreach ($submenus as $menu) {
            $str .= "<li>";
            $str.="<a href=\"$menu[url]\" class=\"J_menuItem\">
            <i class=\"fa fa-angle-double-right\"></i>
            <span class=\"menu-text\">$menu[name]</span></a>";
            $str.= "<li>";
        }
        return $str;
    }
    /**
     * 首页欢迎图
     */
    public function welcome(){
        $img = Db::name('options') -> where(["id"=>1]) -> value("admin_img");
        $this -> assign("img",$img);
        return $this -> fetch();
    }

    // 退出登录
    public function logout()
    {
        insert_admin_log('退出了后台系统');
        session('admin_auth', null);
//        session('admin_auth_sign', null);
        $this->redirect('admin/index/login');
    }
    /**
     * 修改个人信息
     */
    public function profile(){
        if(IS_POST){

            $posts = input("post.");
            $id['id'] =session("admin_auth")["admin_id"];
            if(!$posts['nicename']){
                $this->error("请填写用户昵称!");
            }
            if(!$posts['avatar']){
                $this->error("请上传用户头像!");
            }
            $rst = model('AdminUsers')->allowField(true) -> save($posts,$id);
            if($rst){
                $this->success("修改成功");
            }else{
                $this->error("您未做任何改动或服务器连接失败,请稍后重试!");
            }
        }else{
            $id['id'] = session("admin_auth")["admin_id"];
            $msg = Db::name('admin_users') ->where($id) -> find();
            $this -> assign("msg",$msg);
            return $this -> fetch();
        }
    }
    /**
     * 修改密码
     */
    public function repwd(){
        if(IS_POST){
            $posts = input("post.");
            if($posts['oldPwd'] == $posts['newPwd']){
                $this -> error("您输入的新密码与旧密码一致,无法修改!");
            }
            if($posts['newPwd'] != $posts['newPwd2']){
                $this->error("输入的两次新密码不一致,请重试!");
            }
            $pwd['user_pass'] = md5( $posts['oldPwd']);
            $pwd['id'] = session("admin_auth")["admin_id"];
            $rst = Db::name('admin_users') ->where($pwd) -> count();
            if(!$rst){
                $this -> error("原始密码错误!");
            }
            $user_login = Db::name('admin_users') -> where($pwd) -> value("user_login");
            $user_pass['user_pass'] = md5($posts['newPwd']);
            $rst = Db::name('admin_users') ->where($pwd) -> update($user_pass);
            if($rst){
                adminLog2("修改账户 ".$user_login." 的密码!");
                $this->success("密码修改成功!");
            }else{
                $this->error("服务器繁忙,请稍后重试!");
            }
        }else{
            $id['id'] = session("admin_auth")["admin_id"];
            $msg = Db::name('admin_users') -> where($id) -> find();
            $this -> assign("msg",$msg);
            return $this -> fetch();
        }
    }

    // 清除缓存
    public function clear()
    {
        clear_cache();
        insert_admin_log('清除了系统缓存');
        $this->success('清除成功');
    }
}
